package com.atguigu.gmallsugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmallsugar.bean.*;
import com.atguigu.gmallsugar.service.TradeService;
import com.atguigu.gmallsugar.service.TrafficService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Project:gmallRealTime
 * Package:com.atguigu.gmallsugar.controller
 * Author: ZengHaiFeng
 * CreateTime:2023/5/6 16:50:36
 * Description: Todo
 * Version：1.0
 */
@RestController
public class SugarController {
    @Autowired
    private TradeService tradeService;
    @Autowired
    private TrafficService trafficService;
    @RequestMapping("/sugar/trade/gmv")
    public String gmv(Integer date){
        if (date == null){
            date = Integer.valueOf(
                    new SimpleDateFormat("yyyyMMdd")
                            .format(new Date()));
        }

        BigDecimal gmv = tradeService.gmv(date);
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");
        result.put("data",gmv);
        return result.toJSONString();
    }

    @RequestMapping("/sugar/trade/gmv/spu")
    public String gmvBySpu(Integer date){
        if (date == null) {
            date = Integer.valueOf(
                    new SimpleDateFormat("yyyyMMdd").format(new Date())
            );
        }
        List<Spu> spus = tradeService.gmvBySpu(date);

        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONObject data = new JSONObject();
        JSONArray categories = new JSONArray();
        for (Spu spu : spus) {
            categories.add(spu.getSpu_name());
        }
        data.put("categories",categories);

        JSONArray series = new JSONArray();
        JSONObject obj = new JSONObject();
        obj.put("name","spu name");
        JSONArray data1 = new JSONArray();
        for (Spu spu : spus) {
            data1.add(spu.getOrder_amount());
        }
        obj.put("data",data1);
        series.add(obj);

        data.put("series",series);

        result.put("data",data);


        return result.toJSONString();
    }

    @RequestMapping("/sugar/trade/gmv/tm")
    public String gmvByTm(Integer date){
        if (date == null){
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        List<Tm> list = tradeService.gmvByTm(date);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");


        JSONArray data = new JSONArray();
        for (Tm tm : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", tm.getTrademark_name());
            obj.put("value", tm.getOrder_amount());

            data.add(obj);
        }


        result.put("data", data);

        return result.toJSONString();
    }

    @RequestMapping("/sugar/traffic")
    public String traffic(Integer date){
        if (date == null){
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMDD").format(new Date()));
        }

        List<Traffic> list = trafficService.traffic(date);
        HashMap<Integer, Traffic> hourToTrafficMap = new HashMap<>();
        for (Traffic traffic : list) {
            hourToTrafficMap.put(traffic.getHour(),traffic);
        }

        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONObject data = new JSONObject();
        JSONArray categories = new JSONArray();
        for (int i = 0; i < 24; i++) {
            categories.add(i);
        }
        data.put("categories",categories);

        JSONArray series = new JSONArray();
        
        JSONObject pv = new JSONObject();
        pv.put("name","pv");
        JSONArray pvData = new JSONArray();
        pv.put("data",pvData);
        series.add(pv);

        JSONObject uv = new JSONObject();
        uv.put("name","uv");
        JSONArray uvData = new JSONArray();
        uv.put("data",uvData);
        series.add(uv);

        JSONObject sv = new JSONObject();
        sv.put("name","sv");
        JSONArray svData = new JSONArray();
        sv.put("data",svData);
        series.add(sv);

        data.put("series",series);

        for (int i = 0; i < 24; i++) {
            Traffic traffic = hourToTrafficMap.getOrDefault(i, new Traffic(i, 0L, 0L, 0L));
            pvData.add(traffic.getPv());
            uvData.add(traffic.getUv());
            svData.add(traffic.getSv());
        }

        result.put("data",data);
        return result.toJSONString();
    }

    @RequestMapping("/sugar/traffic/kw")
    public String kw(Integer date){
        if (date == null){
            date = Integer.valueOf(
                    new SimpleDateFormat("yyyyMMdd")
                            .format(new Date()));
        }

        List<Kw> list = trafficService.kw(date);
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONArray data = new JSONArray();
        for (Kw kw : list) {
            JSONObject obj = new JSONObject();
            obj.put("name",kw.getKeyword());
            obj.put("value",kw.getKeyword_count());
            data.add(obj);
        }

        result.put("data",data);
        return result.toJSONString();

    }

    @RequestMapping("/sugar/trade/province")
    public String province(Integer date){
        if (date == null){
            date = Integer.valueOf(
                    new SimpleDateFormat("yyyyMMdd")
                            .format(new Date()));
        }

        List<Province> list = tradeService.province(date);
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONObject data = new JSONObject();
        data.put("valueName","order amount");

        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("order count number");
        data.put("tooltipNames",tooltipNames);

        JSONArray tooltipUnits = new JSONArray();
        tooltipUnits.add("a");
        data.put("tooltipUnits",tooltipUnits);

        JSONArray mapData = new JSONArray();
        for (Province province : list) {
            JSONObject obj = new JSONObject();
            obj.put("name",province.getProvince_name());
            obj.put("value",province.getOrder_amount());

            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(province.getOrder_count());
            obj.put("tooltipValues",tooltipValues);

            mapData.add(obj);
        }
        data.put("mapData",mapData);


        result.put("data",data);
        return result.toJSONString();
    }
}
